package com.zhike.b2b.expo.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.expo.IExpoService;
import com.zhike.b2b.model.ClassVo;
import com.zhike.b2b.model.ExpoClassVo;
import com.zhike.b2b.model.ExpoVo;
import com.zhike.b2b.model.NewsVo;
import com.zhike.b2b.sysparam.IParamService;
import com.zhike.b2b.util.ClassUtil;

@Service
public class ExpoService extends BaseService implements IExpoService {
	
	/**
	 * 根据类别ID查询展会类别
	 * 
	 * @param classId
	 * @return
	 */
	public ClassVo getClassById(String classId) {
		String sql = "select class_id,class_name,list_model,index_model,store_path,home_name,list_rule from expo_class where checked=1  and remove_tag=1 and class_id = ? ";
		return jt.queryForObject(sql, new RowMapper<ClassVo>() {
			public ClassVo mapRow(ResultSet rs, int rowNum) throws SQLException {
				ExpoClassVo ecv = new ExpoClassVo();
				ecv.setClassId(rs.getString("class_id"));
				ecv.setClassName(rs.getString("class_name"));
				ecv.setListRule(rs.getString("list_rule"));
				ecv.setStorePath(rs.getString("store_path"));
				ecv.setIndexModel(rs.getString("index_model"));
				ecv.setListModel(rs.getString("list_model"));
				ecv.setListUrl(ClassUtil.getListUrl(ecv, 1));
				return ecv;
			}
		}, classId);
	}

	/**
	 * 根据条件查询展会列表
	 * 
	 * @param classId
	 * @param limitStr
	 * @param orderCln
	 * @param commend
	 * @return
	 */
	public List<ExpoVo> queryExpos(String classId, String limitStr,
			String orderCln) {
		final String defaultLogoImg = ps.getParamValue("default_img");
		List<ExpoVo> lnc = jt.query(getSqlstr(classId, limitStr, orderCln, "",
				"", ""), new RowMapper<ExpoVo>() {
			public ExpoVo mapRow(ResultSet rs, int rowNum) throws SQLException {
				ExpoVo ev = new ExpoVo();
				ev.setClassId(rs.getString("class_id"));
				ev.setExpoId(rs.getString("expo_id"));
				ev.setListRule(rs.getString("list_rule"));
				ev.setStorePath(rs.getString("store_path"));
				ev.setArticleRule(rs.getString("article_rule"));
				ev.setExpoName(rs.getString("expo_name"));
				String expoImg = rs.getString("expo_photo");
				if(expoImg == null || expoImg.trim().length()<1)
					 expoImg = defaultLogoImg;
				ev.setExpoPhoto(expoImg);
				
                ev.setOperTime(rs.getString("oper_time"));
                ev.setVenue(rs.getString("venue"));
				ev.setUrl(ClassUtil.getObjUrl(ev, ev.getExpoId()));
				return ev;
			}
		});
		return lnc;
	}

	public String getSqlstr(String clzId, String limitStr, String orderStr,
			String commend, String hotExpro, String goodExpo) {
		StringBuffer sqlBuff = new StringBuffer("select distinct el.expo_id,el.expo_name,el.class_id,el.venue ,a.file_path as expo_photo, el.oper_time oper_time ,");
		sqlBuff.append(" store_path , list_rule, article_rule from expo_list el ");
        sqlBuff.append(" left join attach a on el.attach_id = a.attach_root_id  ");
		sqlBuff.append(" inner join expo_class ec on el.class_id=ec.class_id ");
		sqlBuff.append(" where el.remove_tag=1 and el.expo_state =1 ");
		if (!clzId.equals(""))
			sqlBuff.append(" and el.class_id like '" + clzId + "%'");
		if (!commend.equals(""))
			sqlBuff.append(" and el.commend=" + commend);
		if (!hotExpro.equals(""))
			sqlBuff.append(" and el.hot_expo=" + hotExpro);
		if (!goodExpo.equals(""))
			sqlBuff.append(" and el.good_expo=" + goodExpo);

		if (orderStr.equals("")) {
			sqlBuff
					.append(" order by el.recommend  desc  ,el.hot_expo desc ,el.list_top desc,el.expo_id desc  ");
		} else {
			sqlBuff.append(" order by el." + orderStr.toString() + " desc ");
		}
		if (!limitStr.equals(""))
			sqlBuff.append(" limit " + limitStr);
		logger.debug("getSqlstr当前查询的语句为：" + sqlBuff.toString());
		return sqlBuff.toString();
	}

	
}
